﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.DAL;
using Core.Utils;

namespace Core.BOL
{
    public class StatMailsRepository : IDisposable
    {
        private ScoutNetDataContext context = null;

        #region Construtor
        public StatMailsRepository()
        {
            context = GeneralUtils.GetDataContext();
        }
        #endregion

        #region Methodes
        public void AddMail(tMail_Stats_List mail)
        {
            context.tMail_Stats_Lists.InsertOnSubmit(mail);
        }

        public List<vw_Mail_MailsView> SelectMailViews()
        {
            var query = (from m in context.vw_Mail_MailsViews
                         where m.Title != string.Empty
                         orderby m.SID ascending, m.InsertDate descending
                         select m);
                         
            return query.ToList();
        }

        public List<vw_Mail_MailsView> SelectMailViewsBySection(int SID)
        {
            var query = (from m in context.vw_Mail_MailsViews
                         where m.Title != string.Empty && m.SID == SID
                         orderby m.SID ascending, m.InsertDate descending
                         select m);

            return query.ToList();
        }

        public void DeleteMailStat(Guid mailStatID)
        {
            tMail_Stats_List tStatMail = context.tMail_Stats_Lists.SingleOrDefault(p => p.ID == mailStatID);
            if (tStatMail != null)
            {
                context.tMail_Stats_Lists.DeleteOnSubmit(tStatMail);
            }
        }

        public void Save()
        {
            context.SubmitChanges();
        }

        public void Dispose()
        {
            context.Dispose();
        }
        #endregion
    }
}
